package com.framework.service.core.service.impl;


import com.framework.common.Enums.FrameworkErrorCodeEnum;
import com.framework.common.util.FastDFSClient;
import com.framework.common.util.OSSClientUtil;
import com.framework.service.core.service.IFileService;
import com.framework.core.api.response.UploadRes;
import com.framework.service.core.service.IFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;


/**
 * @author zhoubb
 * @version V1.0
 * @Date 2020/5/4 15:40
 * @since JDK 1.8
 */
@Service
@Slf4j
public class FileServiceImpl implements IFileService {

    @Autowired
    private FastDFSClient fastDFSClient;

    @Autowired
    private OSSClientUtil ossClient;

    @Override
    public UploadRes uploadFile(MultipartFile file) {
        UploadRes res = new UploadRes();
        log.info("[文件上传]-文件名=[{}]", file.getOriginalFilename());
        try {
            String filePath = fastDFSClient.uploadFile(file);
            log.info("[文件上传]-完成-文件名=[{}]-文件路径=[{}]", file.getOriginalFilename(), filePath);
            res.setCode(FrameworkErrorCodeEnum.SUCCESS.getCode());
            res.setMsg(FrameworkErrorCodeEnum.SUCCESS.getMsg());
            res.setFilePath(filePath);
            return res;
        } catch (IOException e) {
            res.setCode(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getCode());
            res.setMsg(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getMsg());
            return res;
        }
    }

    @Override
    public UploadRes uploadFileToOss(MultipartFile file) {
        log.info("[文件上传至OSS]-文件名=[{}]", file.getOriginalFilename());
        UploadRes res = new UploadRes();
        if (file.getSize() > 10 * 1024 * 1024) {
            log.info("[文件上传至OSS]-文件过大=[{}]", file.getSize());
            res.setCode(FrameworkErrorCodeEnum.FILE_TOO_LARGE.getCode());
            res.setMsg(FrameworkErrorCodeEnum.FILE_TOO_LARGE.getMsg());
            return res;
        }
        try {
            String filePath = ossClient.uploadFileToOSS(file);
            log.info("[文件上传]-完成-文件名=[{}]-文件路径=[{}]", file.getOriginalFilename(), filePath);
            if(StringUtils.isBlank(filePath)) {
                res.setCode(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getCode());
                res.setMsg(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getMsg());
                return res;
            }
            res.setCode(FrameworkErrorCodeEnum.SUCCESS.getCode());
            res.setMsg(FrameworkErrorCodeEnum.SUCCESS.getMsg());
            res.setFilePath(filePath);
            return res;
        } catch (IOException e) {
            log.info("[文件上传至OSS]-文件上传失败");
            res.setCode(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getCode());
            res.setMsg(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getMsg());
            return res;
        }

    }

    @Override
    public UploadRes uploadOpenFileToOss(MultipartFile file) {
        log.info("[公开文件上传至OSS]-文件名=[{}]", file.getOriginalFilename());
        UploadRes res = new UploadRes();
        if (file.getSize() > 10 * 1024 * 1024) {
            log.info("[公开文件上传至OSS]-文件过大=[{}]", file.getSize());
            res.setCode(FrameworkErrorCodeEnum.FILE_TOO_LARGE.getCode());
            res.setMsg(FrameworkErrorCodeEnum.FILE_TOO_LARGE.getMsg());
            return res;
        }
        try {
            String filePath = ossClient.uploadOpenFileToOSS(file);
            log.info("[公开文件上传]-完成-文件名=[{}]-文件路径=[{}]", file.getOriginalFilename(), filePath);
            if(StringUtils.isBlank(filePath)) {
                res.setCode(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getCode());
                res.setMsg(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getMsg());
                return res;
            }
            res.setCode(FrameworkErrorCodeEnum.SUCCESS.getCode());
            res.setMsg(FrameworkErrorCodeEnum.SUCCESS.getMsg());
            res.setFilePath(filePath);
            return res;
        } catch (IOException e) {
            log.info("[公开文件上传至OSS]-文件上传失败");
            res.setCode(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getCode());
            res.setMsg(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getMsg());
            return res;
        }
    }

    @Override
    public void downloadFileFromOss(String fileName, HttpServletResponse response) {
        try(InputStream is = ossClient.getInputStreamByFileUrl(fileName);
            OutputStream os = response.getOutputStream()) {
//            response.setContentType("image/jpeg");
//            response.addHeader("Content-Length",String.valueOf(is.available()));
//            IOUtils.copy(is, os);
            //读取文件流
            int len = 0;
            byte[] buffer = new byte[1024 * 10];
            while ((len = is.read(buffer)) != -1){
                os.write(buffer,0,len);
            }
            os.flush();
        } catch (Exception e) {
            log.error("下载图片发生异常", e);
        }
    }

    @Override
    public UploadRes uploadOpenFileToOss(InputStream is) {
        UploadRes res = new UploadRes();
        try {
            if (is.available() > 10 * 1024 * 1024) {
                log.info("[公开文件上传至OSS]-文件过大=[{}]", is.available());
                res.setCode(FrameworkErrorCodeEnum.FILE_TOO_LARGE.getCode());
                res.setMsg(FrameworkErrorCodeEnum.FILE_TOO_LARGE.getMsg());
                return res;
            }
            String filePath = ossClient.uploadOpenFile2OSS(is,UUID.randomUUID().toString());
            log.info("[公开文件上传]-完成-文件路径=[{}]", filePath);
            if(StringUtils.isBlank(filePath)) {
                res.setCode(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getCode());
                res.setMsg(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getMsg());
                return res;
            }
            res.setCode(FrameworkErrorCodeEnum.SUCCESS.getCode());
            res.setMsg(FrameworkErrorCodeEnum.SUCCESS.getMsg());
            res.setFilePath(filePath);
            return res;
        } catch (Exception e) {
            log.error("文件上传异常", e);
            res.setCode(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getCode());
            res.setMsg(FrameworkErrorCodeEnum.FILE_UPLOAD_FAIL.getMsg());
            return res;
        }

    }

}
